﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EmpContractEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id) && id != "undefined")
                {
                    var v = ctx.EmpContacts.Single(r => r.Id == int.Parse(id));
                    StartDate.Value = v.StartDate.ToString("yyyy-MM-dd");
                    if(v.EndDate!=null)
                    {
                        EndDate.Value =((DateTime)v.EndDate).ToString("yyyy-MM-dd");
                    }
                    CreatDate.Value = v.CreatDate.ToString("yyyy-MM-dd");
                    CreatAddress.Value = v.CreatAddress;
                    Holder.Value = v.Holder;
                    Remark.Value = v.Remark;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            EmpContact empContact;
            if (!string.IsNullOrEmpty(id) && id != "undefined")
            {
                empContact = ctx.EmpContacts.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                empContact = new EmpContact();
                empContact.EmpId = int.Parse(Request.QueryString["empId"]);
                ctx.EmpContacts.InsertOnSubmit(empContact);
            }
            empContact.StartDate = DateTime.Parse(StartDate.Value);
            empContact.EndDate = DateTime.Parse(EndDate.Value);
            empContact.CreatDate = DateTime.Parse(CreatDate.Value);
            empContact.CreatAddress = CreatAddress.Value;
            empContact.Holder = Holder.Value;
            empContact.Remark = Remark.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + empContact.Id + "';window.close();</script>");
        }
    }
}